package com.yh.rent.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.jasper.tagplugins.jstl.core.Out;

import com.yh.rent.biz.impl.UserBizImpl;
import com.yh.rent.entity.User;

/**
 * Servlet implementation class RegistServlet
 */
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path
				+ "/";
		request.setAttribute("basePath", basePath);
		String username = request.getParameter("name");
		if (username == null || "".equals(username)) {
			request.getRequestDispatcher("/WEB-INF/templates/regs.ftl").forward(request, response);
			return;
		}
		String error = null ;
		List<User> users = new UserBizImpl().getAllUsers() ;
		for (User user : users) {
			if (username.equals(user.getUserName())) {
				 error = "用户名已存在!" ;
				 return ;
			}
		}
		error = "用户名可用!" ;
		PrintWriter out = response.getWriter() ;
		out.write(error);
		out.flush();
		out.close();
		
		String pwd = request.getParameter("password");
		String repwd = request.getParameter("repassword");
		String tel = request.getParameter("telephone");
		String realname = request.getParameter("username");
		
	}

}
